#include "napkinTrick.h"


//--------------------------------------------------------------
void napkinTrick::setup(){
	
	sprintf(name, "napkin");
	
	bTakeSnapshot = false;
	bHaveBlobs = false;
	
	minBlobSize = 20;
	maxBlobSize = 1000;
	
	for (int i = 0; i < MAX_NUM_BLOBS; i++){
		bDrawBlob[i] = true;
		blobs[i].setup(width, height);
	}
	
}

//--------------------------------------------------------------
void napkinTrick::reset(){
	bTakeSnapshot = false;
	bHaveBlobs = false;
	
	minBlobSize = 20;
	maxBlobSize = 1000;
	
	for (int i = 0; i < MAX_NUM_BLOBS; i++){
		bDrawBlob[i] = true;
		blobs[i].angle = 0;
		blobs[i].bTriggered = false;
		blobs[i].meAsGray.set(0);
	}
	
	for (int i = 0; i < nBlobs; i++){
		blobs[i].clear();
	}
	
	
}

//--------------------------------------------------------------
void napkinTrick::update(){
	
	
	if (bTakeSnapshot == true){
		vbd->takeSnapshot();
		nBlobs = vbd->nBlobs;
		if (vbd->nBlobs > 0){
			for (int i = 0; i < nBlobs; i++){
				// does this work, ie, can you memcopy
				// or better do a hard copy
				// of every field:
				
				
				/*
					ofCvBlob 			blob;
					videoTexture 		myTexture;	

					//----------------------------------------- translating points
					ofPoint2f			pos;
					float 				angle;
					float 				scale;
					int					nPts;						
					ofPoint2f			pts[MAX_CONTOUR_LENGTH];
					ofPoint2f 			texPts[4];

				*/
				
				//memcpy(&blobs[i],&vbd->vidBlob[i], sizeof(videoBlob));
			
				
					//or, maybe better???
					
					blobs[i].blob = vbd->vidBlob[i].blob;
					blobs[i].myTexture = vbd->vidBlob[i].myTexture;
					blobs[i].pos = vbd->vidBlob[i].pos;
					blobs[i].angle = vbd->vidBlob[i].angle;
					blobs[i].scale = vbd->vidBlob[i].scale;
					blobs[i].nPts = vbd->vidBlob[i].nPts;
					for (int j = 0; j < blobs[i].nPts; j++){
						blobs[i].pts[j] = vbd->vidBlob[i].pts[j];
					}
					
					for (int j = 0; j < 4; j++){
						blobs[i].texPts[j] = vbd->vidBlob[i].texPts[j];
					}
					blobs[i].clear();
					blobs[i].meAsGray.set(0);
				
			
			}
			bHaveBlobs = true;
		}
		bTakeSnapshot = false;
	}
	
	meAsGray.set(0);
	meAndedWithHand.set(0);
	
	if (bHaveBlobs == true){
		
		for (int i = 0; i < nBlobs; i++){
			
			blobs[i].checkCoverage(vhd);
			
			
			blobs[i].update();
			if (blobs[i].bTriggered){
				bDrawBlob[i] = (i % 2 == 0) ? false : true;
				blobs[i].angle = blobs[i].randomAngle;
				blobs[i].bTriggered = false;
			}
			
			if (blobs[i].bTraveling){
				blobs[i].pos.x = 0.97f * blobs[i].pos.x + 
								 0.03 * blobs[i].targetx;
				blobs[i].pos.y = 0.97f * blobs[i].pos.y + 
								 0.03 * blobs[i].targety;
				blobs[i].energy = 0.97f * blobs[i].energy + 
								 0.03 * 1.0f;
			}
		}
	}
	
}

//--------------------------------------------------------------
void napkinTrick::draw(){
	if (bHaveBlobs == true){
		for (int i = 0; i < nBlobs; i++){
			if (bDrawBlob[i] == true){
				if (blobs[i].energy > 0){
					ofEnableAlphaBlending();
					ofSetColor(255,255,255, (1-blobs[i].energy) * 255);
				}
				
				blobs[i].draw();
				if (blobs[i].energy > 0){
					ofDisableAlphaBlending();
					ofSetColor(0xffffff);
				}
				//blobs[i].meAsGray.draw(0,i*250);
			}
		}
	}
	
}


//--------------------------------------------------------------
void napkinTrick::keyPressed(int key){
	if (key == 's'){
		bTakeSnapshot = true;
		
		for (int i = 0; i < MAX_NUM_BLOBS; i++){
			bDrawBlob[i] = true;
		}
	
		
	} else if (key == '0'){
		
		for (int i = 0; i < nBlobs; i++){
			bDrawBlob[i] = (i % 3 == 0) ? false : true;
		}
		
		for (int i = 0; i < nBlobs; i++){
			blobs[i].angle = ofRandom(-0.5f, 0.5f);
		}
		
	} else if (key == ' '){
		bHaveBlobs = false;
	} else if (key == '1'){
		for (int i = 0; i < nBlobs; i++){
			blobs[i].bTraveling = true;
		}
	}
}